home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 December / maximum-cd-2009-12.iso / DiscContents / gimp-2.7.0-i686-setup.exe / {app} / lib / gimp / 2.0 / plug-ins / palette-sort.py < prev    next >
Encoding:
Python Source  |  2009-08-19  |  2.6 KB  |  74 lines

  1. #!/usr/bin/env python
  2. #
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15.  
  16. from gimpfu import *
  17.  
  18. gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
  19.  
  20. def palette_sort (palette, model, channel, ascending):
  21.     #If palette is read only, work on a copy:
  22.     editable = pdb.gimp_palette_is_editable(palette)
  23.     if not editable:palette = pdb.gimp_palette_duplicate (palette)
  24.  
  25.     num_colors = pdb.gimp_palette_get_info (palette)
  26.     entry_list = []
  27.     for i in xrange (num_colors):
  28.         entry =  (pdb.gimp_palette_entry_get_name (palette, i),
  29.                   pdb.gimp_palette_entry_get_color (palette, i))
  30.         index = entry[1][channel]
  31.         if model == "HSV":
  32.             index = entry[1].to_hsv()[channel]
  33.         else:
  34.             index = entry[1][channel]
  35.         entry_list.append ((index, entry))
  36.     entry_list.sort(lambda x,y: cmp(x[0], y[0]))
  37.     if not ascending:
  38.         entry_list.reverse()
  39.     for i in xrange(num_colors):
  40.         pdb.gimp_palette_entry_set_name (palette, i, entry_list[i][1][0])
  41.         pdb.gimp_palette_entry_set_color (palette, i, entry_list[i][1][1])
  42.  
  43.     return palette
  44.  
  45.  
  46. register(
  47.     "python-fu-palette-sort",
  48.     N_("Sort the colors in a palette"),
  49.     "palette_merge (palette, model, channel, ascending) -> new_palette",
  50.     "Joao S. O. Bueno Calligaris, Carol Spears",
  51.     "Joao S. O. Bueno Calligaris",
  52.     "2006",
  53.     N_("_Sort Palette..."),
  54.     "",
  55.     [
  56.         (PF_PALETTE, "palette",  _("Palette"), ""),
  57.         (PF_RADIO,   "model",    _("Color _model"), "HSV",
  58.                                     ((_("RGB"), "RGB"),
  59.                                      (_("HSV"), "HSV"))),
  60.         (PF_RADIO,   "channel",  _("Channel to _sort"), 2,
  61.                                     ((_("Red or Hue"),          0),
  62.                                      (_("Green or Saturation"), 1),
  63.                                      (_("Blue or Value"),       2))),
  64.         (PF_BOOL,   "ascending", _("_Ascending"), True)
  65.     ],
  66.     [],
  67.     palette_sort,
  68.     menu="<Palettes>",
  69.     domain=("gimp20-python", gimp.locale_directory)
  70.     )
  71.  
  72.  
  73. main ()
  74.